package com.mt.channel.service.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.mt.channel.constants.ChannelConstant;
import com.mt.channel.entity.ChannelAccountEntity;
import com.mt.channel.entity.ChannelAdminEntity;
import com.mt.channel.entity.ChannelAdminRoleRelateEntity;
import com.mt.channel.entity.ChannelEntity;
import com.mt.channel.mapper.ChannelAccountEntityMapper;
import com.mt.channel.mapper.ChannelAdminEntityMapper;
import com.mt.channel.mapper.ChannelAdminRoleRelateMapper;
import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;
import com.mt.common.util.ConvertUtil;

@Component
public class ChannelAdminServiceModel {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ChannelAdminServiceModel.class);

    @Autowired
    private ChannelAdminEntityMapper channelAdminEntityMapper;

    @Autowired
    private ChannelServiceModel channelServiceModel;

    @Autowired
    private ChannelAccountServiceModel channelAccountServiceModel;

    @Autowired
    private ChannelAdminRoleRelateMapper channelAdminRoleRelateMapper;

    @Autowired
    private ChannelAccountEntityMapper channelAccountEntityMapper;

    public ServiceResult<List<ChannelAdminEntity>> selectList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        Assert.notNull(this.channelAdminEntityMapper, "Property 'channelAdminEntityMapper' is required.");
        ServiceResult<List<ChannelAdminEntity>> result = new ServiceResult<List<ChannelAdminEntity>>();
        try {
            if (pagerInfo != null) {
                PageHelper.startPage(pagerInfo.getPageIndex(), pagerInfo.getPageSize());
            }
            List<ChannelAdminEntity> ChannelAdminEntityResult = this.channelAdminEntityMapper.selectList(params);
            result.setResult(ChannelAdminEntityResult);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelAdminServiceModel][selectList]：query selectList occur exception", e);
        }
        return result;
    }

    public ServiceResult<ChannelAdminEntity> selectInfo(Map<String, Object> params) {
        Assert.notNull(this.channelAdminEntityMapper, "Property 'channelAdminEntityMapper' is required.");
        ServiceResult<ChannelAdminEntity> result = new ServiceResult<ChannelAdminEntity>();
        Map<String, Object> map = null;
        boolean channelAdmin = false;
        try {
            if (params.containsKey("ChannelAdminSelectAllBoolean")) {
                channelAdmin = ConvertUtil.toBool(params.get("ChannelAdminSelectAllBoolean"), false);
            }
            ChannelAdminEntity channelAdminEntity = this.channelAdminEntityMapper.selectByPrimaryKey(params);
            if (channelAdminEntity != null && channelAdmin) {
                map = new HashMap<String, Object>();
                map.put("channelSelectAllByChannelIdBoolean", true);
                map.put("channelId", channelAdminEntity.getChannelId());
                ServiceResult<ChannelEntity> channelEntityServiceResult = channelServiceModel.selectInfo(map);
                if (channelEntityServiceResult.getResult() != null && channelEntityServiceResult.getSuccess()) {
                    ChannelEntity channelEntity = channelEntityServiceResult.getResult();
                    if (channelEntity.getIsEncrypt().equals(ChannelConstant.CHANNEL_IS_ENCRYPT_2)) {
                        map = new HashMap<String, Object>();
                        map.put("channelId", channelAdminEntity.getChannelId());
                        List<ChannelAccountEntity> channelAccountEntityList = channelAccountEntityMapper
                            .selectList(map);
                        if (channelAccountEntityList.size() > 0) {
                            channelEntity.setChannelAccountEntity(channelAccountEntityList.get(0));
                        }
                    }

                    channelAdminEntity.setChannelEntity(channelEntity);
                }

            }
            result.setResult(channelAdminEntity);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelAdminServiceModel][selectInfo]：query selectInfo occur exception", e);
        }
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public ServiceResult<Integer> insert(ChannelAdminEntity channelAdminEntity) {
        Assert.notNull(this.channelAdminEntityMapper, "Property 'channelAdminEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelAdminEntityMapper.insert(channelAdminEntity);
            if (save > 0) {
                id = channelAdminEntity.getChannelAdminId();
                List<ChannelAdminRoleRelateEntity> channelAdminRoleList = channelAdminEntity.getChannelAdminRoleList();
                if (channelAdminRoleList != null && channelAdminRoleList.size() > 0) {
                    for (ChannelAdminRoleRelateEntity channelAdminRoleRelateEntity : channelAdminRoleList) {
                        channelAdminRoleRelateEntity.setChannelAdminId(id);
                    }
                    channelAdminRoleRelateMapper.insertList(channelAdminRoleList);
                }
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelAdminServiceModel][insert]：query insert occur exception", e);
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }

    public ServiceResult<Integer> updateRelate(ChannelAdminEntity channelAdminEntity) {
        Assert.notNull(this.channelAdminEntityMapper, "Property 'channelAdminEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelAdminEntityMapper.updateByPrimaryKeySelective(channelAdminEntity);
            if (save > 0) {
                id = channelAdminEntity.getChannelAdminId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelAdminServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateByPrimaryKeySelective(ChannelAdminEntity channelAdminEntity) {
        Assert.notNull(this.channelAdminEntityMapper, "Property 'channelAdminEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelAdminEntityMapper.updateByPrimaryKeySelective(channelAdminEntity);
            if (save > 0) {
                id = channelAdminEntity.getChannelAdminId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelAdminServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> deleteByPrimaryKey(Integer channelAdminId) {
        Assert.notNull(this.channelAdminEntityMapper, "Property 'channelAdminEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int save = 0;
        try {
            save = this.channelAdminEntityMapper.updateByPrimaryKey(channelAdminId);
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelAdminServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }
}
